package com.book.controller;


import com.book.pojo.Comment;
import com.book.result.Result;
import com.book.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxy
 * @since 2023-10-12 17:15:29
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

   //作者查看自己的评论
    @GetMapping("selectCommentByAuthorId")
    public Result selectCommentByAuthorId(@RequestParam Long authorId){
        List<Comment> list = commentService.selectAuthorComment(authorId);
        return Result.OK().setData(list);
    }

    //作者可以查看每本自己作品的所有评论（包括作者本身评论） 递归查询
    @GetMapping("selectCommentByBookIdZtree")
    public Result selectCommentByBookId(@RequestParam Long bookId){
        List<Map<String, Object>>  list = commentService.selectCommentByBookIdZtree(bookId);
        return Result.OK().setData(list);
    }

    //作者发布评论 增加
    @PostMapping("addComment")
    public Result addComment(@RequestBody Comment comment){
        commentService.addComment(comment);
        return Result.OK();
    }

    //作者删除自己评论 递归删除
    @PostMapping("deleteComment")
    public Result deleteComment(@RequestParam Long id){
        commentService.deleteComment(id);
        return Result.OK();
    }

    //点赞 (前台传递+1,-1)进行修改点赞数
    @PutMapping("updateLike")
    public Result updateLike(@RequestParam Comment comment){
        commentService.updateLike(comment);
        return Result.OK();
    }
}
